<style type="less">
    .login {
        height: 92rpx;
        width: 600rpx;
        background: #006EFF;
        border-radius: r16px;
        border-radius: r16px;
        font-family: PingFangSC-Regular;
        font-size: 18px;
        color: #FFFFFF;
        letter-spacing: 0;
        line-height: 92rpx;
        padding: 0;
    }
    .logo-img {
        height: 424rpx;
        width: 505rpx;
    }
    .logo {
        text-align: center;
        padding-top: 120rpx;
    }
    .slogan {
        font-family: PingFangSC-Medium;
        font-size: 18px;
        color: #333333;
        text-align: center;
        line-height: 50rpx;
        margin-top: 60rpx;
    }
    .sub-slogan {
        font-family: PingFangSC-Regular;
        font-size: 14px;
        color: #999999;
        text-align: center;
        line-height: 22px;
    }
    .layout-margintop{
        margin-top: 40rpx;
    }
    .layout-marginbottom{
        margin-bottom: 120rpx;
    }
</style>

<template>
    <view>
        <view class="logo">
            <image src="/static/images/logo.png" class="logo-img" />
        </view>
        <view class="slogan">
            即时通信IM · 直播电商 解决方案
        </view>
        <view class="sub-slogan layout-margintop">
            体验发送直播弹幕、送礼、点赞、购买
        </view>
        <view class="sub-slogan layout-marginbottom">
            消息等直播电商功能。
        </view>
        <view>
            <button class="login" bindgetuserinfo="getUserinfo" open-type="getUserInfo">登录</button>
        </view>
    </view>
</template>

<script>
    import wepy from '@wepy/core';
    import CONST from '@/common/const'
    wepy.component({
        props: {},
        data: {
            userData: {},
            seesionKey: '',
            loading: true
        },
        methods: {
            check() {
                wx.login({
                    success: (data) => {
                        wx.request({
                            url: `${CONST.HOST}/getUser`,
                            method: 'POST',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                                code: data.code
                            },
                            success: (data) => {
                                if (data.data.code == -2) {
                                    this.seesionKey = data.data.seesionKey;
                                } else {
                                    this.userData = data.data;
                                }
                            },
                            fail: () => {},
                            complete: () => {
                                this.loading = false;
                            }
                        });
                    }
                });
            },
            getUserinfo(data) {
                wx.showLoading({
                    mask: true
                })
                const encryptedData = data.$wx.detail.encryptedData;
                const iv = data.$wx.detail.iv;
                wx.request({
                    url: `${CONST.HOST}/wxRegister`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                        session_key: this.seesionKey,
                        encryptedData: encryptedData,
                        iv: iv
                    },
                    success: (data) => {
                        this.userData = data.data;
                        this.$emit('loginSuccess')
                    },
                    fail: () => {},
                    complete() {
                        wx.hideLoading()
                    }
                });
            }
        },
        computed: {},
        created() {
            this.check()
        }
    });
</script>
